package search;

/**
 * @Author leecho
 * @Date: 2021/10/20 12:19
 * @Version 1.0
 */

public class InsertSearch {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,10,11,12,13};
        InsertSearch insertSearch = new InsertSearch();
        int i = insertSearch.insertSearch(arr, 0, arr.length - 1, 1);
        System.out.println(i);

    }

    public int insertSearch(int[] arr, int left, int right, int findVal){
        if (left > right)
            return -1;
        // 核心代码
        int mid = left + (right - left) * (findVal - arr[left]) / (arr[right] - arr[left]);

        if (findVal > arr[mid]){
            return insertSearch(arr,mid+1,right,findVal);
        } else if (findVal < arr[mid]){
            return insertSearch(arr,right,mid-1,findVal);
        } else {
            return mid;
        }
    }

}
